package com.example.a19859218698_homework2;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.ImageView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class Uc3Activity extends AppCompatActivity {

    private ImageView[] imageViews;
    private int currentIndex = 0;
    private Handler handler = new Handler();
    private Runnable runnable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_uc3);

        // 初始化图片视图数组
        imageViews = new ImageView[]{
                findViewById(R.id.image1),
                findViewById(R.id.image2),
                findViewById(R.id.image3),
                findViewById(R.id.image4),
                findViewById(R.id.image5)
        };

        // 加载图片资源
        int[] imageResources = {
                R.drawable.image1,  // 请确保在res/drawable中有这些图片
                R.drawable.image2,
                R.drawable.image3,
                R.drawable.image4,
                R.drawable.image5
        };

        for (int i = 0; i < imageViews.length; i++) {
            imageViews[i].setImageResource(imageResources[i]);
        }

        // 显示第一张图片
        showImage(currentIndex);

        // 定时切换图片
        runnable = new Runnable() {
            @Override
            public void run() {
                currentIndex = (currentIndex + 1) % imageViews.length;
                showImage(currentIndex);
                handler.postDelayed(this, 2000); // 2秒切换一次
            }
        };
        handler.postDelayed(runnable, 2000);

        // 返回按钮
        findViewById(R.id.btn_back).setOnClickListener(v -> finish());
    }

    private void showImage(int index) {
        for (int i = 0; i < imageViews.length; i++) {
            imageViews[i].setVisibility(i == index ? View.VISIBLE : View.GONE);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 移除回调，防止内存泄漏
        handler.removeCallbacks(runnable);
    }
}